﻿using System;
using System.Collections.Generic;
using System.Text;
using EnvDTE;
using EnvDTE80;

namespace dFactor2.CodeHelpers
{
    public abstract class CodeHelperBase
    {
        protected DTE2 ApplicationObject { get; set; }

        public CodeHelperBase(DTE2 applicationObject)
        {
            this.ApplicationObject = applicationObject;
        }

        public static List<vsCMElement> GetCurrentElementType(DTE2 applicationObject)
        {
            TextSelection textSel = applicationObject.ActiveDocument.Selection as TextSelection;
            TextPoint textPnt = (TextPoint)textSel.ActivePoint;

            List<vsCMElement> currentTypes = new List<vsCMElement>();

            foreach (vsCMElement supType in Enum.GetValues(typeof(vsCMElement)))
            {
                CodeElement codeElement = textPnt.get_CodeElement(supType);
                if (codeElement != null)
                {
                    currentTypes.Add(supType);
                }
            }

            return currentTypes;
        }

        protected virtual T GetCurrentCodeElement<T>(vsCMElement expectedType) where T : class
        {
            TextSelection textSel = ApplicationObject.ActiveDocument.Selection as TextSelection;
            TextPoint textPnt = (TextPoint)textSel.ActivePoint;

            T codeElement = textPnt.get_CodeElement(expectedType) as T;
            return codeElement;
        }

        protected virtual void AddCodeToFunction(CodeFunction func, string code)
        {
            EditPoint edtpnt = func.GetStartPoint(vsCMPart.vsCMPartBody).CreateEditPoint();
            edtpnt.Insert(code);

            edtpnt.SmartFormat(func.StartPoint);
            edtpnt.SmartFormat(func.EndPoint);
        }

    }
}
